﻿using System.ComponentModel;
using System.Collections.ObjectModel;
using System.Net;
using System;
using System.Linq;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using SoccerLiveScores.Exceptions;
using SoccerLiveScores.Extensions;
using SoccerLiveScores.Models;

namespace SoccerLiveScores.ViewModels
{
    public class MainViewModel : INotifyPropertyChanged
    {
        private Competitions Competitions = new Competitions();

        public bool IsDataLoaded
        {
            get 
            {
                var vm = GetListPageViewModel();
                return vm.IsDataLoaded;
            }
        }

        public MainViewModel()
        {
            var listPageViewModels = GetListPageViewModels();
            PageCollection = new ObservableCollection<PageViewModel>();
            foreach (var listPageViewModel in listPageViewModels)
            {
                PageCollection.Add(listPageViewModel);
            }
        }

        private List<ListPageViewModel> GetListPageViewModels()
        {
            var list = new List<ListPageViewModel>();
            Competitions.ForEach(c => 
            { 
                list.Add(new ListPageViewModel { TitleText = c.Name, Url= c.Url });
            });
            return list;
        }

        public void LoadData()
        {
            var vm = GetListPageViewModel();
            vm.LoadData();
        }

        private ListPageViewModel GetListPageViewModel()
        {
            return PageCollection[0] as ListPageViewModel;
        }

       private ObservableCollection<PageViewModel> _pageCollection;

       public ObservableCollection<PageViewModel> PageCollection
       {
           get { return _pageCollection; }
           set
           {
               if (_pageCollection != value)
               {
                   _pageCollection = value;
                   NotifyPropertyChanged("PageCollection");
               }
           }
       }

        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(String propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (null != handler)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}